﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class wucGvList : System.Web.UI.UserControl
{
    public DataSet ds;

    public string equalsStr = "";

    public int lineCount = 0;

    public void setDs(DataSet ds)
    {
        this.ds = ds;
    }

    public DataSet getDs()
    {
        return ds;
    }

    public DataSet Ds
    { 
        get { return ds;}
        set{ ds = value;}
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //ArrayList tableList = new ArrayList();

        DataTable formatTable = new DataTable();

        formatTable.Columns.Add("id");
        formatTable.Columns.Add("name");
        formatTable.Columns.Add("content");
        formatTable.Columns.Add("addTime");
        formatTable.Columns.Add("price");
        formatTable.Columns.Add("class");
        formatTable.Columns.Add("pic");

        int dataSetCount = ds.Tables.Count;

        for (int i = 0; i < dataSetCount; i++)
        {
            DataTable table = (DataTable)ds.Tables[i];

            for (int j = 0; j < table.Rows.Count; j++)
            {
                DataRow oneRow = formatTable.NewRow();
                oneRow["id"] = table.Rows[j][0];
                oneRow["name"] = table.Rows[j][1];
                oneRow["content"] = table.Rows[j][2];
                oneRow["addTime"] = table.Rows[j][3];
                oneRow["price"] = table.Rows[j][4];
                oneRow["class"] = table.Rows[j][5];
                oneRow["pic"] = table.Rows[j][6];

                formatTable.Rows.Add(oneRow);

                DataRow aRow = formatTable.NewRow();
                aRow["id"] = table.Rows[j][0];
                aRow["name"] = table.Rows[j][1];
                aRow["content"] = table.Rows[j][2];
                aRow["addTime"] = table.Rows[j][3];
                aRow["price"] = table.Rows[j][4];
                aRow["class"] = table.Rows[j][5];
                aRow["pic"] = table.Rows[j][6];

                formatTable.Rows.Add(aRow);

                if (j == 0)
                {
                    DataRow newRow = formatTable.NewRow();
                    newRow["id"] = table.Rows[j][0];
                    newRow["name"] = table.Rows[j][1];
                    newRow["content"] = table.Rows[j][2];
                    newRow["addTime"] = table.Rows[j][3];
                    newRow["price"] = table.Rows[j][4];
                    newRow["class"] = table.Rows[j][5];
                    newRow["pic"] = table.Rows[j][6];

                    formatTable.Rows.Add(newRow);
                }

            }
        }

        GridView1.DataSource = formatTable;
        GridView1.DataBind();

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
        {
            string str = ((DataRowView)e.Row.DataItem).Row["class"].ToString();

            if (equalsStr.Equals(str))
            {
                if ((lineCount == 0) || (lineCount % 2 == 0))
                {
                    //pic
                    Image Image1 = (Image)e.Row.FindControl("Image1");
                    Image1.Visible = true;
                    string imgAddr = ((DataRowView)e.Row.DataItem).Row["pic"].ToString();
                    Image1.ImageUrl = imgAddr;

                    //name
                    Label LBL_Name = (Label)e.Row.FindControl("LBL_Name");
                    LBL_Name.Visible = false;
                    //string strName = ((DataRowView)e.Row.DataItem).Row["name"].ToString();
                    //LBL_Name.Text = strName;

                    //addtime
                    Label LBL_Time = (Label)e.Row.FindControl("LBL_Time");
                    LBL_Time.Visible = true;
                    string addTime = ((DataRowView)e.Row.DataItem).Row["addTime"].ToString();
                    LBL_Time.Text = addTime;

                    //content
                    TextBox TXT_Content = (TextBox)e.Row.FindControl("TXT_Content");
                    TXT_Content.Visible = true;
                    string content = ((DataRowView)e.Row.DataItem).Row["content"].ToString();
                    TXT_Content.Text = content;

                    //price
                    Label LBL_Price = (Label)e.Row.FindControl("LBL_Price");
                    LBL_Price.Visible = true;
                    string price = ((DataRowView)e.Row.DataItem).Row["price"].ToString();
                    LBL_Price.Text = price;

                    //buyNumber
                    TextBox TXT_count = (TextBox)e.Row.FindControl("TXT_count");
                    TXT_count.Visible = true;
                    TXT_count.Text = "0";

                    lineCount++;
                }
                else
                {
                    Image Image1 = (Image)e.Row.FindControl("Image1");
                    Image1.Visible = false;

                    //name
                    Label LBL_Name = (Label)e.Row.FindControl("LBL_Name");
                    LBL_Name.Visible = true;
                    string strName = ((DataRowView)e.Row.DataItem).Row["name"].ToString();
                    LBL_Name.Text = strName;

                    //Label LBL_Name = (Label)e.Row.FindControl("LBL_Name");
                    //LBL_Name.Visible = false;

                    Label LBL_Time = (Label)e.Row.FindControl("LBL_Time");
                    LBL_Time.Visible = false;

                    TextBox TXT_Content = (TextBox)e.Row.FindControl("TXT_Content");
                    TXT_Content.Visible = false;

                    Label LBL_Price = (Label)e.Row.FindControl("LBL_Price");
                    LBL_Price.Visible = false;

                    TextBox TXT_count = (TextBox)e.Row.FindControl("TXT_count");
                    TXT_count.Visible = false;

                    lineCount++;
                }                
            }
            else
            {
                Image Image1 = (Image)e.Row.FindControl("Image1");
                Image1.Visible = false;

                Label LBL_Name = (Label)e.Row.FindControl("LBL_Name");
                //LBL_Name.Visible = false;

                Label LBL_Time = (Label)e.Row.FindControl("LBL_Time");
                LBL_Time.Visible = false;

                TextBox TXT_Content = (TextBox)e.Row.FindControl("TXT_Content");
                TXT_Content.Visible = false;

                Label LBL_Price = (Label)e.Row.FindControl("LBL_Price");
                LBL_Price.Visible = false;

                TextBox TXT_count = (TextBox)e.Row.FindControl("TXT_count");
                TXT_count.Visible = false;

                //className
                LBL_Name.Text = "　　　" + str;
                LBL_Name.Font.Size = 16;
                LBL_Name.Font.Bold = true;

                equalsStr = str;

                lineCount = 0;
            }
        }
    }
}
